package com.demoku.web.web.test;

import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iff.infra.util.JsonHelper;
import org.iff.infra.util.mybatis.plugin.Page;
import org.iff.infra.util.mybatis.plugin.PageVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demoku.web.application.test.UserApplication;
import com.demoku.web.application.test.vo.UserVO;
import com.demoku.web.web.common.BaseController;

@Controller
public class UserController extends BaseController {

	@Inject
	UserApplication userApplication;

	@RequestMapping("/userIndex")
	public String index() {
		return "index";
	}
	
	@RequestMapping("/toAddUser.do")
	public String toAddUser(HttpServletRequest request, HttpServletResponse response) {
		return "test/test/addUser";
	}

	@RequestMapping("/addUser.do")
	public String addUser(UserVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		userApplication.saveUser(vo);
		
		modelMap.addAttribute("message", "录入成功！");
		modelMap.addAttribute("toPage", "/pageFindAllUser.do");
		return "common/auth_messages";
	}
	
	@RequestMapping("/deleteUser.do")
	public String deleteUser(String id, UserVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		userApplication.removeUser(id);
		modelMap.addAttribute("message", "删除成功！");
		modelMap.addAttribute("toPage", "/pageFindAllUser.do");
		
		return "common/auth_messages";
	}
	
	@RequestMapping("/getUserInfo.do")
	public String getAuthUserInfo(String id, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		modelMap.addAttribute("user", userApplication.getUserVO(id));
		return "test/test/infoUser";
	}
	
	@RequestMapping("/toEditUser.do")
	public String toEditUser(String id, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		modelMap.addAttribute("user", userApplication.getUserVO(id));
		return "test/test/editUser";
	}
	
	@RequestMapping("/editUser.do")
	public String editUser(UserVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		userApplication.saveUser(vo);
		modelMap.addAttribute("message", "编辑成功！");
		modelMap.addAttribute("toPage", "/pageFindAllUser.do");
		return "common/auth_messages";
	}

	@RequestMapping("/pageFindAllUser.do")
	public String pageFindAllAccount(UserVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		
		int currentPage = 0;
		if(request.getParameter("currentPage") != null)
			currentPage = Integer.valueOf(request.getParameter("currentPage"));
		
		Page pages = Page.pageable(10, currentPage, 0, null);
		Page page = userApplication.pageFindAllUser(vo, pages);
		modelMap.addAttribute("page", page);
		modelMap.addAttribute("user", vo);
		
		return "test/test/pageFindAllUser";
	}
		
}
